/**
 * 
 */

function dispAdminView() {

	$("#dispCont .grid").hide();

	$("#b1txt").text("Search Complaints");
	$("#b2txt").text("Resolution Time");
	$("#b3txt").text("Dept Reports");
	//$("#b4txt").text("X");
	
	$("#SideBarButton4").hide();

	$("#SideBarButton1").off().click(function() {
		dispSearchComplaints();
	});
	$("#SideBarButton2").off().click(function() {
		dispReportByResolutionTime();
	});
	$("#SideBarButton3").off().click(function() {
		dispDeptCompPieChart();
	});
	$("#SideBarButton4").off().click(function() {

	});

	dispSearchComplaints();

}

function dispSearchComplaints() {

	$("#dispCont .grid").hide();
	
	$("#SideBarButton1").removeClass("info").addClass("warning");
	$("#SideBarButton2").removeClass("warning").addClass("info");
	$("#SideBarButton3").removeClass("warning").addClass("info");
	$("#SideBarButton4").removeClass("warning").addClass("info");
	
	$("#divSearchComplaint").slideDown();
	$("#complaintsTable tbody").empty();

	$("#searchComplaint").off().submit(
			function() {

				console.log("search");
				$("#complaintsTable tbody").empty();
				$.ajax({
					type : "POST",
					url : "/ComplaintReg/rest/service/searchComplaints",
					data : $("#searchComplaint").serialize(),
					success : function(data) {

						console.log(data);

						$.each(data, function(key, val) {
							var row = "<tr><td>" + val.cid + "</td><td>" + val.location + "</td><td>" + val.date_submission + "</td><td>"
									+ val.description + "</td><td><button class=" + "userEdit" + ">Edit</button></tr>";
							$("#complaintsTable tbody").append(row);

							$("#ComplaintList").slideDown();
						});

						var $rows = $('#complaintsTable tr').not('thead tr');
						$('#InputCompTableSearch').keyup(function() {
							var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();

							$rows.show().filter(function() {
								var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
								return !~text.indexOf(val);
							}).hide();
						});

						$(".userEdit").click(function() {
							var row = $(this).parents('tr').get(0);
							console.log($(row.cells[0]).text().trim());

							var status = editComplaint($(row.cells[0]).text().trim());

						});

					}
				});

				return false;
			});

}

function dispReportByResolutionTime() {

	$("#dispCont .grid").hide();
	$("#divChartContainer").slideDown();

	$("#SideBarButton1").removeClass("warning").addClass("info");
	$("#SideBarButton2").removeClass("info").addClass("warning");
	$("#SideBarButton3").removeClass("warning").addClass("info");
	$("#SideBarButton4").removeClass("warning").addClass("info");

	var reportData = [];

	$.getJSON("/ComplaintReg/rest/service/getReportByResolutionTime", function(data) {
		$.each(data, function(key, value) {
			reportData.push(value);
		});

		var chart = AmCharts.makeChart("chartdiv", {
			"type" : "pie",
			"theme" : "none",
			"title" : "Avg resolution time",
			"dataProvider" : reportData,
			"valueField" : "value",
			"titleField" : "Label",
			"radius" : "30%",
			"innerRadius" : "50%",
			"exportConfig" : {
				menuItems : [ {
					icon : '/lib/3/images/export.png',
					format : 'png'
				} ]
			}
		});
	});
}

